<template>
	<view>
		<view class="fixed">
			<!-- 状态栏 -->
			<view class="statusBar" :style="'height:'+statusBarHeight+'px'"></view>
			<view class="myheader padding-sm  flex justify-between">
				<view class="font-sub-title">
					<text class="cuIcon-back margin-right-ssm" @click="back()"></text>
					我的设备
				</view>
			</view>
		</view>
		<view class="cu-list menu" :style="{marginTop:navHeight}">
			<view class="cu-item arrow" @click="jump('/pages/user/userInfo/userInfo')">
				<view class="content">
					<text class="iconfont font-sub-title text-orange margin-right">&#xe628;</text>
					<text>设备基本参数</text>
				</view>
			</view>
			<!-- <view class="cu-item arrow">
				<view class="content">
					<text class="iconfont font-sub-title text-blue margin-right">&#xed36;</text>
					<text>主题</text>
				</view>
				<view class="action text-gray">
					<picker mode="selector" :range="themeList" :value="themeIndex" @change="bindThemeChange">
						<view>{{themeList[themeIndex]}}</view>
					</picker>
				</view>
			</view> -->
			<view class="cu-item arrow" @click="jump('/pages/equipment/longsite')">
				<view class="content">
					<text class="iconfont font-sub-title text-olive margin-right">&#xe62f;</text>
					<text>久坐提醒</text>
				</view>
				<view class="action text-gray">
					{{longsiteOpen?'开启':'关闭'}}
				</view>
			</view>
			<view class="cu-item arrow" @click="jump('/pages/equipment/alarm')">
				<view class="content">
					<text class="iconfont font-sub-title text-orange margin-right">&#xe602;</text>
					<text>闹钟</text>
				</view>
				<view class="action text-gray">
					
				</view>
			</view>
			<view class="cu-item arrow">
				<view class="content">
					<text class="iconfont font-sub-title text-purple margin-right">&#xe623;</text>
					<text>健康状况自动检测</text>
				</view>
				<view class="action text-gray">
					<picker mode="selector" :range="autoList" :value="autoIndex" @change="bindAutoChange">
						<view v-if="autoList[autoIndex]!='0min'">{{autoList[autoIndex]}}</view>
						<view v-else>关闭</view>
					</picker>
				</view>
			</view>
			<view class="cu-item arrow" @click="jump('/pages/equipment/tempalarm')">
				<view class="content">
					<text class="iconfont font-sub-title text-pink margin-right">&#xe695;</text>
					<text>体温监测</text>
				</view>
				<view class="action text-gray">
					{{tempAutoOpen?'开启':'关闭'}} 
				</view>
			</view>
			<view class="cu-item arrow" @click="jump('/pages/equipment/antilose')">
				<view class="content">
					<text class="iconfont font-sub-title text-grey margin-right">&#xe624;</text>
					<text>防丢提醒</text>
				</view>
				<view class="action text-gray">
					{{antiloseOpen?'开启':'关闭'}}
				</view>
			</view>
			<view class="cu-item arrow" @click="jump('/pages/equipment/heartalarm')">
				<view class="content">
					<text class="iconfont font-sub-title text-red margin-right">&#xe603;</text>
					<text>危险心率告警</text>
				</view>
				<view class="action text-gray">
					{{heartList[heartIndex]}}
				</view>
			</view>
			<view class="cu-item" >
				<view class="content">
					<text class="iconfont font-sub-title text-brown margin-right">&#xe600;</text>
					<text>抬腕亮屏</text>
				</view>
				<view class="action text-gray">
					<switch class="blue" @change="open" :class="isopen?'checked':''" :checked="isopen"></switch>
				</view>
			</view>
			<view class="cu-item arrow" >
				<view class="content">
					<text class="iconfont font-sub-title text-green margin-right">&#xe601;</text>
					<text>佩戴位置</text>
				</view>
				<view class="action text-gray">
					<picker mode="selector" :range="handList" :value="handIndex" @change="bindHandChange">
						<view>{{handList[handIndex]}}</view>
					</picker>
				</view>
			</view>
			<view class="cu-item arrow">
				<view class="content">
					<text class="iconfont font-sub-title text-yellow margin-right">&#xe6a5;</text>
					<text>屏幕亮度</text>
				</view>
				<view class="action text-gray">
					<picker mode="selector" :range="lightList" :value="lightIndex" @change="bindLightChange">
						<view>{{lightList[lightIndex]}}</view>
					</picker>
				</view>
			</view>
			<view class="cu-item arrow" @click="jump('/pages/equipment/nodisturb')">
				<view class="content">
					<text class="iconfont font-sub-title text-cyan margin-right">&#xe671;</text>
					<text>勿扰设置</text>
				</view>
				<view class="action text-gray">
					{{nodisturbOpen?'开启':'关闭'}}
				</view>
			</view>
			<view class="cu-item arrow">
				<view class="content">
					<text class="iconfont font-sub-title text-blue margin-right">&#xe740;</text>
					<text>语言设置</text>
				</view>
				<view class="action text-gray">
					<picker mode="selector" :range="languageList" :value="languageIndex" @change="bindLanguageChange">
						<view>{{languageList[languageIndex]}}</view>
					</picker>
				</view>
			</view>
			<view class="cu-item arrow" @click="restoreFactory">
				<view class="content">
					<text class="iconfont font-sub-title text-black margin-right">&#xe738;</text>
					<text>恢复出产设置</text>
				</view>
				<view class="action text-gray">
					
				</view>
			</view>
			<view class="cu-item" @click="close">
				<view class="text-red font-normal" style="text-align: center;margin: 0 auto;">
					<text>断开连接</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	const bandSDK = uni.requireNativePlugin('BandSDK')
	var that;
	import {
		mapGetters
	} from 'vuex'
	export default {
		computed:{
			...mapGetters(['navHeight']),
		},
		data() {
			return {
				statusBarHeight: Vue.prototype.StatusBar,
				themeList:[],
				themeIndex:0,
				languageList:['英文','中文'],
				languageIndex:0,
				isopen:uni.getStorageSync("raiseScreen")||false,
				autoList:['0min','10min','20min','30min','40min','50min','60min'],
				autoIndex:uni.getStorageSync("autoIndex")||1,
				handList:['左手','右手'],
				handIndex:uni.getStorageSync("handIndex")||0,
				lightList:['低','中','高'],
				lightIndex:uni.getStorageSync("lightIndex")||1,
				heartList:['100','110','120','130','140','150','关闭'],
				heartIndex:uni.getStorageSync('heartAlarmIndex')||6,
				antiloseOpen:uni.getStorageSync("antiloseOpen")||false,
				tempAutoOpen:uni.getStorageSync("autoTemp")||true,
				longsiteOpen:uni.getStorageSync("longsiteOpen")||false,
				nodisturbOpen:uni.getStorageSync("nodisturbOpen")||false
			}
		},
		onShow(){
			that = this;
			this.isopen=uni.getStorageSync("raiseScreen")||false
			this.autoIndex=uni.getStorageSync("autoIndex")||1
			this.handIndex=uni.getStorageSync("handIndex")||0
			this.lightIndex=uni.getStorageSync("lightIndex")||1
			this.heartIndex=uni.getStorageSync('heartAlarmIndex')
			if(this.heartIndex==null||this.heartIndex==undefined){
				this.heartIndex=6
			}
			this.nodisturbOpen=uni.getStorageSync("nodisturbOpen")||false
			this.antiloseOpen=uni.getStorageSync("antiloseOpen")||false
			this.tempAutoOpen=uni.getStorageSync("autoTemp")||true
			this.longsiteOpen=uni.getStorageSync("longsiteOpen")||false
			bandSDK.initClient(res=>{
				bandSDK.getThemeInfo(res=>{
					var len = res.themeTotal;
					that.themeIndex = res.themeCurrentIndex;
					for(var i=0;i<len;i++){
						that.themeList.push("主题"+i)
					}
				})
			})
		},
		methods: {
			bindLanguageChange(e){
				this.languageIndex = e.target.value
				bandSDK.settingLanguage({
					'langType':e.target.value
				},res=>{
					if(res=='success'){
						uni.showToast({
							"title":"保存成功"
						})
					}
				})
			},
			restoreFactory(){
				uni.showModal({
				    title: '温馨提示',
				    content: '您确定恢复出厂设置吗？',
				    success: function (res) {
				        if (res.confirm) {
				            bandSDK.settingRestoreFactory({},res=>{
				            	if(res=='success'){
				            		uni.showToast({
				            			title:"恢复出厂设置成功",
										position:"bottom",
										icon:"none"
				            		})
				            		uni.clearStorage();
									setTimeout(function(){
										uni.reLaunch({
											url:'/pages/login/login'
										})
									},1000)
				            	}
				            })
				        }
				    }
				});
				
			},
			open(e){
				this.isopen = e.target.value
				bandSDK.settingRaiseScreen({
					on:that.isopen?'1':'0'
				},res=>{
					if(res=='success'){
						uni.setStorageSync("raiseScreen",this.isopen)
						uni.showToast({
							title:"保存成功"
						})
					}
				})
			},
			back(){
				uni.navigateBack();
			},
			jump(page){
				uni.navigateTo({
					url:page
				})
			},
			close(){
				uni.showModal({
					title: '温馨提示',
					content: '您确定要断开手环连接吗？',
					success: function (res) {
					    if (res.confirm) {
							uni.removeStorageSync("mac")
					        bandSDK.disconnectBle()
							uni.setStorageSync("userDisconnect",true)
							uni.removeStorageSync("bindMac")
							uni.removeStorageSync("bindName")
							uni.navigateBack();
					    }
					}
				})
			},
			bindThemeChange(e){
				this.themeIndex = e.target.value
				bandSDK.settingMainTheme({
					theme:e.target.value
				},res=>{
					if(res=='success'){
						uni.showToast({
							"title":"保存成功"
						})
					}
				})
			},
			bindAutoChange(e){
				this.autoIndex = e.target.value
				var auto = this.autoList[this.autoIndex]
				var time = auto.substring(0,auto.length-3)
				var mode
				if(time=='0'){
					mode =0
				}else{
					mode = 1
				}
				bandSDK.settingHeartMonitor({
					mode:mode,
					intervalTime:time
				},res=>{
					if(res=='success'){
						uni.showToast({
							"title":"保存成功"
						})
						uni.setStorageSync("autoIndex",e.target.value)
					}
				})
			},
			bindHandChange(e){
				this.handIndex = e.target.value
				bandSDK.settingHandWear({
					leftOrRight:e.target.value
				},res=>{
					if(res=='success'){
						uni.showToast({
							"title":"保存成功"
						})
						uni.setStorageSync("handIndex",e.target.value)
					}
				})
			},
			bindLightChange(e){
				this.lightIndex = e.target.value
				bandSDK.settingDisplayBrightness({
					level:e.target.value
				},res=>{
					if(res=='success'){
						uni.showToast({
							"title":"保存成功"
						})
						uni.setStorageSync("lightIndex",e.target.value)
					}
				})
			}
		}
	}
</script>

<style>

</style>
